package com.example.demo.service.impl;

import com.example.demo.dao.UserDao;
import com.example.demo.entity.UserEntity;
import com.example.demo.result.Result;
import com.example.demo.result.ResultUtil;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserDao userDao;

    @Override
    public Result enroll(UserEntity userEntity) {
        if (userDao.selectAuthority(userEntity.getOperatorId()) == 0){
            userDao.enroll(userEntity);
            return ResultUtil.success("注册成功");
        }

        return ResultUtil.success();
    }

    @Override
    public Result login(UserEntity userEntity) {
       if (userDao.selectUser(userEntity.getUsername()) != null){
           UserEntity userEntity1 = userDao.selectPasswordByUsername(userEntity.getUsername());
           if (userEntity.getPassword().equals(userEntity1.getPassword())){
               return ResultUtil.success(userEntity);
           }else {
               return ResultUtil.error("用户名或密码错误，请重新登录！");

           }
       }else {
           return ResultUtil.error("该用户不存在");
       }
    }
    //权限验证
    @Override
    public Result selectAuthority(String username) {

        if (userDao.selectAuthority(username)==0){
            return ResultUtil.success("该用户为超级管理员用户");
        }
        return ResultUtil.success("该用户为普通用户");
    }

}
